package com.yy.jhl.ui.manager.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.yy.common.commonutils.ImageLoaderUtils;
import com.yy.jhl.R;

import java.util.ArrayList;

/**
 * Created by libingn on 2018/1/5.
 */

public class PictureAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private int selectedPosition = -1;
    private ArrayList<String> selectPaths = new ArrayList<>();
    private Context mContext;
    private boolean isShowAdd = true;


    public void setSelectPaths(ArrayList<String> selectPaths) {
        this.selectPaths = selectPaths;
        notifyDataSetChanged();
    }

    public PictureAdapter(Context context,boolean isShowAdd) {
        mContext = context;
        this.isShowAdd = isShowAdd;
        inflater = LayoutInflater.from(context);
    }

    public int getCount() {
        if (isShowAdd){
            if(selectPaths.size() == 9){
                return 9;
            }
            return (selectPaths.size() + 1);
        }else {
            return selectPaths.size();
        }
    }

    public void setShowAdd(boolean showAdd) {
        isShowAdd = showAdd;
        notifyDataSetChanged();
    }

    public Object getItem(int arg0) {
        return null;
    }

    public long getItemId(int arg0) {
        return 0;
    }

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
//        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_published_grida,
                    parent, false);
            holder = new ViewHolder();
            holder.image = (ImageView) convertView
                    .findViewById(R.id.item_grida_image);
//            convertView.setTag(holder);
//        } else {
//            holder = (ViewHolder) convertView.getTag();
//        }
//            if (selectPaths.size() == 1 && isShowAdd){
//                if (position == 1){
//                    holder.image.setImageResource(R.mipmap.add_icon);
//                }else {
//                    ImageLoaderUtils.display(mContext,holder.image,selectPaths.get(0));
//                }
//            }else {
                if (position ==selectPaths.size() && isShowAdd) {
                    holder.image.setImageResource(R.mipmap.add_icon);
                    if (position == 9) {
                        holder.image.setVisibility(View.GONE);
                    }
                } else if (selectPaths.get(position).endsWith("mp4")){
                    //小视频
                    holder.image.setImageResource(R.mipmap.video_play_icon);
                }else {
                    ImageLoaderUtils.display(mContext,holder.image,selectPaths.get(position));
                }
//            }
        return convertView;
    }

    public class ViewHolder {
        public ImageView image;
    }

}